package com.our.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import javax.websocket.server.PathParam;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.our.biz.RewardTypeBiz;
import com.our.biz.RewardsBiz;
import com.our.biz.userProBiz;
import com.our.model.RewardType;
import com.our.model.Rewards;
import com.our.model.UserPro;
import com.our.model.Users;
import com.our.util.BusinessException;

@Controller
@RequestMapping("/mycount")
public class PricountController {

	@Resource
	private userProBiz userProBiz;
	@Resource
	private RewardTypeBiz rewardTypeBiz;
	@Resource
	private RewardsBiz rewardsBiz;

	@RequestMapping("/mycountcen")
	public String mycountcen(Map<String, Object> request, HttpSession session) throws BusinessException {
		Users user = (Users) session.getAttribute("currentUser");
		int userId = user.getUserId();
		List<UserPro> userPros = userProBiz.findByUserId(userId);
		System.out.println(userPros.size());
		float waitget = 0F;
		float getMoney = 0F;
		float waitget1 = 0F;
		float getMoney1 = 0F;
		int count = rewardTypeBiz.countReward(userId);
		Calendar now = Calendar.getInstance();
		request.put("date", now.get(Calendar.DAY_OF_MONTH));
		request.put("count", count);
		if (user.getMoneyAll()==0) {
			return "/mycount/mycountcenter/mycountcen";
		}
		if (!userPros.isEmpty()) {
			for (UserPro userPro : userPros) {
				if (userPro.getProState() == 1 || userPro.getProState() == 2) {
					waitget += userPro.getInvestMoney();
					getMoney += userPro.getGetsMoney();
				} else {
					waitget1 += userPro.getInvestMoney();
					getMoney1 += userPro.getGetsMoney();
				}
			}
		}
		user.setMoneyAll(waitget + user.getMoneyRest());
		request.put("waitget", waitget);
		request.put("waitget1", waitget1);
		request.put("getMoney", getMoney);
		request.put("getMoney1", getMoney1);
		return "/mycount/mycountcenter/mycountcen1";
	}

	@RequestMapping("/invest")
	public String invest(Map<String, Object> request, HttpSession session) throws BusinessException {
		Users user = (Users) session.getAttribute("currentUser");
		int userId = user.getUserId();
		System.out.println(userId);
		List<UserPro> userPros = userProBiz.findByUserId(userId);
		request.put("list", userPros);
		return "/mycount/investmemory/investall";
	}

	@RequestMapping("/invest1")
	public String invest1(@PathParam("state") int state, Map<String, Object> request, HttpSession session)
			throws BusinessException {
		Users user = (Users) session.getAttribute("currentUser");
		int userId = user.getUserId();
		List<UserPro> userPros = userProBiz.findByUserId(userId);
		List<UserPro> list = new ArrayList<UserPro>();
		if (!userPros.isEmpty()) {

			for (UserPro userPro : userPros) {
				if (userPro.getProState() == state) {
					list.add(userPro);
				}
			}
		}
		request.put("list", list);
		if (state == 1) {
			return "/mycount/investmemory/investsuccess";
		} else if (state == 2) {
			return "/mycount/investmemory/investon";
		} else {
			return "/mycount/investmemory/investback";
		}
	}

	@RequestMapping("/detailmoney1")
	public String detailMoney1(String[] state, Map<String, Object> request, HttpSession session, HttpServletRequest req)
			throws BusinessException {
		Users user = (Users) session.getAttribute("currentUser");
		int userId = user.getUserId();
		String[] state1 = req.getParameterValues("state");
		List<UserPro> userPros = userProBiz.findByUserId(userId);
		List<UserPro> list = new ArrayList<UserPro>();
		List<UserPro> list1 = new ArrayList<UserPro>();
		List<UserPro> list2 = new ArrayList<UserPro>();
		if (!userPros.isEmpty()) {
			for (UserPro userPro : userPros) {
				if (userPro.getProState() == 1 || userPro.getProState() == 2) {
					list.add(userPro);
				} else {
					list1.add(userPro);
				}
				list2.add(userPro);
			}
		}
		if (state1.length == 2) {
			request.put("list", list);
			return "/mycount/detailmoney/detailmoneyin";
		} else if (state1.length == 3) {
			request.put("list", list2);
			return "/mycount/detailmoney/detailmoneyall";
		}
		{
			request.put("list", list1);
			return "/mycount/detailmoney/detailmoneyout";
		}

	}

	@RequestMapping("/myreward1")
	public String myreward1(Map<String, Object> request, HttpSession session, @PathParam("rewardId") int rewardId,
			@PathParam("state") int state) throws BusinessException {
		Users user = (Users) session.getAttribute("currentUser");
		int userId = user.getUserId();
		List<RewardType> rewardTypes = rewardTypeBiz.findByUserIdAndRewardId1(userId, rewardId);
		List<RewardType> list = new ArrayList<RewardType>();
		List<RewardType> list1 = new ArrayList<RewardType>();
		List<RewardType> list2 = new ArrayList<RewardType>();
		int count = 0;
		int count1 = 0;
		double number=0;
		Date now =new Date();
		if (!rewardTypes.isEmpty()) {

			for (RewardType rewardType : rewardTypes) {
				if (rewardType.getTypeState() == 1 || rewardType.getTypeState() == 2) {
					list.add(rewardType);
					count++;
					if (rewardType.getTypeState() == 2) {
						count1++;
					}
				} else if (rewardType.getTypeState() == 3) {
					list1.add(rewardType);
				} else {
					number=Math.floor((now.getTime()-rewardType.getUseTime().getTime())/(3600*1000*24));
					list2.add(rewardType);
				}
			}
		}
		request.put("count", count);
		request.put("count1", count1);
		if (state == 1) {
			request.put("list", list);
			if (rewardId == 2) {
				return "mycount/reward/raiserate/useful";
			} else if (rewardId == 3) {
				return "mycount/reward/cashreward/useful";
			} else {
				return "mycount/reward/discount/useful";
			}
		} else if (state == 2) {
			request.put("number", number);
			request.put("list", list2);
			if (rewardId == 2) {
				return "mycount/reward/raiserate/useless";
			} else if (rewardId == 3) {
				return "mycount/reward/cashreward/useless";
			} else {
				return "mycount/reward/discount/useless";
			}
		} else {
			request.put("list", list1);
			if (rewardId == 2) {
				return "mycount/reward/raiserate/outtime";
			} else if (rewardId == 3) {
				return "mycount/reward/cashreward/outtime";
			} else {
				return "mycount/reward/discount/outtime";
			}
		}
	}

	@RequestMapping("/myreward")
	public String myreward2(Map<String, Object> request, HttpSession session, @PathParam("rewardId") int rewardId,
			@PathParam("state") int state) throws BusinessException {
		Users user = (Users) session.getAttribute("currentUser");
		int userId = user.getUserId();
		List<RewardType> rewardTypes = rewardTypeBiz.findByUserIdAndRewardId1(userId, rewardId);
		List<RewardType> list = new ArrayList<RewardType>();
		List<RewardType> list1 = new ArrayList<RewardType>();
		float rewards = 0F;
		if (!rewardTypes.isEmpty()) {

			for (RewardType rewardType : rewardTypes) {
				if (rewardType.getTypeState() == 1 || rewardType.getTypeState() == 2) {
					rewards += rewardType.getTypeNum();
					list.add(rewardType);
				} else {
					list1.add(rewardType);
				}
			}
		}
		request.put("rewards", rewards);
		if (state == 1) {
			request.put("list", list);
			return "/mycount/reward/myrewards";
		} else {
			request.put("list", list1);
			return "/mycount/reward/myrewards1";
		}
	}
	@RequestMapping("/privinfo")
	public String privinfo(Map<String, Object> request,HttpSession session) {
		Users users=(Users) session.getAttribute("currentUser");
		return "/mycount/mycountcenter/privinfo";
	}
}
